package com.company.cloud.base.client;

import cn.hutool.core.util.StrUtil;
import com.company.cloud.base.modules.api.models.BizCodeModel;
import com.company.cloud.base.modules.api.service.BaseClientService;
import com.company.cloud.base.modules.cache.models.ParameterModel;
import com.company.cloud.base.modules.cache.service.BizCodeCacheService;
import com.company.cloud.base.modules.cache.service.ParameterCacheService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseClientResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Service
public class BaseClientServiceImpl implements BaseClientService {

    @Autowired
    private BizCodeCacheService bizCodeCacheService;

    @Autowired
    private ParameterCacheService parameterCacheService;

    @Override
    public BaseClientResult<BizCodeModel> getBizCode(BizCodeModel param) {
        try {
            ValidatorUtils.validate(param);
            BizCodeModel data= bizCodeCacheService.getBizCode(param);
            return BaseClientResult.success(data);
        }catch (Exception e){
            e.printStackTrace();;
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<String> updateBizCode(BizCodeModel param) {
        try {
            ValidatorUtils.validate(param);
            bizCodeCacheService.updateBizCode(param);
            return BaseClientResult.success();
        }catch (Exception e){
            e.printStackTrace();;
            return BaseClientResult.error(e);
        }

    }

    @Override
    public BaseClientResult<String> getParaVal(String paraKey) {
        try {
             if(StrUtil.isEmpty(paraKey)){
                 return BaseClientResult.success();
             }
             ParameterModel  entity= parameterCacheService.getParameterByKey2Cache(paraKey);
             if(entity==null){
                 return BaseClientResult.success();
             }
            return BaseClientResult.success(entity.getParaVal());
        }catch (Exception e){
            e.printStackTrace();;
            return BaseClientResult.error(e);
        }
    }
}
